package com.spc.car.data.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.spc.car.common.utils.StringUtils;
import com.spc.car.data.entity.sys.SysMessage;
import com.spc.car.data.mapper.sys.SysMessageMapper;
import com.spc.car.data.service.sys.SysMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统内部消息 服务实现类
 * </p>
 *
 * @author
 * @since 2020-03-24
 */
@Service
public class SysMessageServiceImpl extends ServiceImpl<SysMessageMapper, SysMessage> implements SysMessageService {


    @Override
    public  int updateMarkReads(long receiverId , String ids){

        String [] list = StringUtils.isBlank(ids) ? null : ids.split(",");

        UpdateWrapper<SysMessage> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("receiver_id" , receiverId)
                .in(ArrayUtils.isNotEmpty(list),"id" , list);
        return baseMapper.update(new SysMessage().setMarkRead(1) , updateWrapper);

    }

    @Override
    public int unreadCount(long userId) {
        QueryWrapper<SysMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id" , userId).eq("mark_read" ,0);
        return baseMapper.selectCount(queryWrapper);
    }
}
